using UnityEngine;
using System.Collections;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;


public class SABattleMapManager : Singleton< SABattleMapManager > 
{

	[ HideInInspector ] public int maxXGrid = 0;
	[ HideInInspector ] public int maxYGrid = 0;

	[ HideInInspector ] public int mapID = GameDefine.INVALID_ID;

	private SAMapData activeMapData = null;
	private Camera mainCamera = null;
	
	private SAMapGrid mapGrid = new SAMapGrid();
	
	private float mapSacle = 0.0f;
	private float maxXGridReal = 0.0f;
	private float maxYGridReal = 0.0f;
	private float maxHalfXGridReal = 0.0f;
	private float maxHalfYGridReal = 0.0f;
	
	[ HideInInspector ] public GameObject terrainLayer = null;

	[ HideInInspector ] public bool isLoead = false;
	
	private int asyncLoadDelay = 0;
	
	public SAMapData getActiveMapData()
	{
		return activeMapData;
	}


	void Start()
	{
		if ( mInstance == null )
		{
			mInstance = this;
			
			// init here ,

			terrainLayer = GameObject.FindWithTag( "terrainLayer" );

			mainCamera = GameObject.FindWithTag( "MainCamera" ).camera;
			mainCamera.orthographicSize = 1.0f / GameSetting.activeRes.battleShowSacle;

			mapSacle = mainCamera.orthographicSize;

			maxXGridReal = mapSacle * GameSetting.activeRes.sizeInPixel.x;
			maxYGridReal = mapSacle * GameSetting.activeRes.sizeInPixel.y;
			maxHalfXGridReal = 0.5f * maxXGridReal + SAMapGrid.GridX;
			maxHalfYGridReal = 0.5f * maxYGridReal + SAMapGrid.GridY;
			
			maxXGrid = GameSetting.activeRes.maxXGrid;
			maxYGrid = GameSetting.activeRes.maxYGrid;
			
			loadMapAsync();
		}
		
		
	}
	
	IEnumerator loadMapCoroutine()
	{
		asyncLoadDelay++;
		
		if ( asyncLoadDelay == 1 ) 
		{
			// delay second.. or some load effects
			
			yield return new WaitForSeconds( 0.001f );
		}
		
		loadMap( GamePlayerData.instance.BattleMapID );
		mapID = GamePlayerData.instance.BattleMapID;
		
		isLoead = true;
		
		GamePlayerData.instance.onLoadMap();
		
		LoadingUIHandler.instance.UnShow();
		
		yield return 1;
	}
	
	public void loadMapAsync()
	{
		asyncLoadDelay = 0;
		
		//LoadingUIHandler.instance.Show();
		
		StartCoroutine( loadMapCoroutine() );
	}
	

	public int getIndex( int x , int y )
	{
		return y * activeMapData.maxX + x;
	}
	
	public int getXWithIndex( int index )
	{
		return index % activeMapData.maxX;
	}
	
	public int getYWithIndex( int index )
	{
		return index / activeMapData.maxX;
	}


	void moveMapReal( int x , int y )
	{
		mainCamera.transform.localPosition = new Vector3( (float)x - SAMapGrid.GridXHalf , (float)y , 0 );
	}
	
	
	void moveMap( int x , int y )
	{
		if ( activeMapData == null ) 
		{
			return;
		}
		
		mapGrid.setValue( x , y );

		moveMapReal( (int)mapGrid.realPosX , (int)mapGrid.realPosY );

		int minX = 0;
		int minY = 0;
		
		int maxX = 20;
		int maxY = 20;
		
		for ( int i = minY ; i < maxY ; i++ )
		{
			for ( int j = minX ; j < maxX ; j++ )
			{
				int index = getIndex( j , i );

				if ( activeMapData.terrain[ index ] > 0 )
				{
					GameObject obj = SASprite.CreateBattleTerrainSprite();
					SASprite sprite = obj.GetComponent< SASprite >();
					sprite.setTerrainID( activeMapData.terrain[ index ] );
					sprite.setPos( j , i );
				}
			}
		}

		
	}
	
	void activeMap()
	{
		if ( activeMapData != null ) 
		{
			
		}
		
		
		
	}

	void clearMap()
	{
		if ( activeMapData == null ) 
		{
			return;
		}

		activeMapData = null;
		
		isLoead = false;
		
		mapID = GameDefine.INVALID_ID;
	}
	
	
	void loadMap( int id )
	{
		clearMap();
		
		activeMapData = new SAMapData();
		
		string path = "BattleMap/battle" + id.ToString() + ".mapz";
		
		FileStream stream = File.Open( GameSetting.StreamingAssetsPath + path , FileMode.Open , FileAccess.Read );
		
		if ( stream == null )
		{
			return;
		}
		
		byte[] com = new byte[ stream.Length ];
		stream.Read( com , 0 , (int)stream.Length );
		byte[] dcom = GameDefine.DeCompress( com );
		Array.Reverse( dcom );
		
		int pos = 0;
		
		activeMapData.maxX = 20;
		activeMapData.maxY = 20;	
		
		activeMapData.terrain = new ushort[ activeMapData.maxX * activeMapData.maxY ];
		byte[] block = new byte[ activeMapData.maxX * activeMapData.maxY ];
		
		for ( int i = activeMapData.maxY - 1 ; i >= 0 ; i-- ) 
		{
			for ( int j = activeMapData.maxX - 1 ; j >=0 ; j-- ) 
			{
				int index = i * activeMapData.maxX + j;
				int tID = BitConverter.ToUInt16( dcom , pos ); pos += 2;
				activeMapData.terrain[ index ] = (ushort)tID;
				
				SATexture texture = SATextureConfig.instance.getTerrainTexture( tID );
				
				if ( texture != null ) 
				{
					texture.initData();
					//texture.loadDataAsync();
				}
				
				if ( texture == null || ( texture.flag != 1 && texture.flag != 45 ) ) 
				{
					if ( texture != null && texture.flag > 0 ) 
					{
						block[ index ] = SAPathFinder.BLOCK1;
					}
					block[ index ] = SAPathFinder.BLOCK1;
				}
			}
		}
		
			
		moveMap( activeMapData.maxX / 2, activeMapData.maxY / 2 );

		Debug.Log( activeMapData.maxX + " " + activeMapData.maxY );
		
		stream.Close();
	}
	
	
}


